package com.hd.trans.dialog

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.hd.trans.R
import com.hd.trans.TranslationInterior
import com.hd.trans.databinding.DialogTongSettingBinding
import com.hd.trans.network.PreferenceMgr
import com.hd.trans.utils.TrackerForTrans

class TongSettingDialog: DialogFragment(), View.OnClickListener {
    private lateinit var mDataBinding: DialogTongSettingBinding
    var confirm: ((Boolean)->Unit)? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //设置dialog样式
        setStyle(STYLE_NO_TITLE, R.style.BottomDialogStyle)
    }

    override fun onStart() {
        super.onStart()
        dialog?.window!!.setLayout(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        dialog?.window!!.setGravity(Gravity.BOTTOM)
        dialog?.setCanceledOnTouchOutside(true)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        mDataBinding = DialogTongSettingBinding.inflate(inflater,container,false)

        dialog?.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) //设置背景为透明
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE)

        initView()
        return mDataBinding.root
    }

    private fun initView() {
        mDataBinding.apply {
            swicth.isChecked = PreferenceMgr.getInstance().isAllowSaveChecked
            mDataBinding.tvConfirm.setOnClickListener(this@TongSettingDialog)
        }
    }

    override fun onClick(v: View) {
        when(v){
            mDataBinding.tvConfirm -> {
                val isChecked = mDataBinding.swicth.isChecked
                confirm?.invoke(isChecked)
                dismiss()
            }
        }
    }

    override fun show(manager: FragmentManager, tag: String?) {
        //视图埋点
        TranslationInterior.getTrackerListener().view(TrackerForTrans.VIEW_SET_RECORDING)
        super.show(manager, tag)
    }
}